 <?php
	include ("header.php");
	require_once ("lib/manageruser.php");
	
	?>
<?php

	$pageTitle = "View User List";
	$divId = "userdetails";
	$handlerPage = "lib/manageuserhandler.php";
	
	$userManager = new ManageUsers ();
	$users = $userManager->getUsers ();
	?>
<script type="text/javascript">
function updateDetails(){

	var handlerPage="<?PHP echo $handlerPage; ?>";
	var divId="<?PHP echo $divId; ?>";
	
	//Delete all rows except the first row(index 0)
	$("#"+divId+" > div > div> table > tbody > tr").each(function(index){
		if(index > 0){
			$(this).remove();
		}
	});

	$.getJSON(handlerPage+"?fulldetails=yes",function(data,status){
		$.each(data, function(counter, user) {
			var userId=user.id;
			var userName=user.userName;
			var userFirstName=user.firstName;
			var userLastName=user.lastName;
			var userEmail=user.email;
			var userCompany=user.company;
			var userUnit=user.unit;
			var userRole=user.role;
			var modifiable=user.modifiable;
			var deletable=user.deletable;

			var bodyelement=$("#"+divId+" > div > div> table > tbody");
			var updateStr= '<a id="userid_'+userId+ '" href="#" onclick="javascript:updateDialog(\'Update User\',\''+handlerPage+'\',\''+handlerPage+'\',\''+userId+'\',\'updateDetails()\')"><span>Edit User</span><div class="modify"></div></a>';
			var deleteStr= '<a id="userid_'+userId+ '" href="#" onclick="javascript:deleteDialog(\'Delete User\',\''+handlerPage+'\',\''+handlerPage+'\',\''+userId+'\',\'updateDetails()\')"><span>Delete User</span><div class="delete"></div></a>';
			var rowString='<tr><td>'+(counter+1)+'</td><td>'+userName+'</td><td>'+userFirstName+'</td><td>'+userLastName+'</td><td>'+userEmail+'</td><td>'+userCompany+'</td><td>'+userUnit+'</td><td>'+userRole+'</td><td>'+modifiable+'</td><td>'+deletable+'</td><td>'+updateStr+'</td><td>'+deleteStr+'</td></tr>';
			$(rowString).appendTo(bodyelement);
			
			var pane = $('#'+divId+'');
			var api = pane.data('jsp');
			api.reinitialise();
			
		});
	});
	
}
</script>
<div class="maincontent">
	<!------- Header ---------->
	<div class="borderedDiv">
		<table style="width: 100%">
			<tr>
				<td class="titleText blueheadertext">View User</td>
			</tr>
		</table>
	</div>


	<!------- Content ---------->

	<div class="scroll-pane displaytable" id="<?PHP echo $divId;?>">
		<table class="contenttable">
			<thead>
				<tr>
					<td style="width: 8%">Sl. No.</td>
					<td>User</td>
					<td>First Name</td>
					<td>Last Name</td>
					<td>Email</td>
					<td>Company Name</td>
					<td>Unit Name</td>
					<td>Role</td>
					<td>Modifiable</td>
					<td>Deletable</td>
					<td style="width: 20px">&nbsp;</td>
					<td style="width: 20px">&nbsp;</td>
				</tr>
			</thead>

			<tbody>
				<tr>
					<td colspan="12" style="text-align: right;"><a href="#"
						onClick="javascript:createDialog('Create User', '<?PHP echo $handlerPage;?>','<?PHP echo $handlerPage;?>','updateDetails()')"><span>Add
								User</span>
						<div id="addUser" class="add" style="float: right;"></div> </a></td>
				</tr>
<?php
$i = 1;
foreach ( $users as $user ) {
	$userId = $user->getUserId ();
	$userName = $user->getUserName ();
	$userFirstName = $user->getFirstName ();
	$userLastName = $user->getLastName ();
	$userEmail = $user->getEmail ();
	$userCompanyName = $user->getCompanyName ();
	$userUnitName = $user->getUnitName ();
	$userRole = $user->getRoleName ();
	$userModifiable = $user->getDeletable ();
	$userDeletable = $user->getModifiable ();
	
	echo ("<tr>");
	echo ("<td>" . $i . "</td>");
	echo ("<td>" . $userName . "</td>");
	echo ("<td>" . $userFirstName . "</td>");
	echo ("<td>" . $userLastName . "</td>");
	echo ("<td>" . $userEmail . "</td>");
	echo ("<td>" . $userCompanyName . "</td>");
	echo ("<td>" . $userUnitName . "</td>");
	echo ("<td>" . $userRole . "</td>");
	echo ("<td>" . $userModifiable . "</td>");
	echo ("<td>" . $userDeletable . "</td>");
	echo "<td><a id=\"userid_" . $userId . "\" alt=\"Edit\" href=\"#\" onClick=\"javascript:updateDialog('Update User', '" . $handlerPage . "','" . $handlerPage . "'," . $userId . ",'updateDetails()')\"><span>Edit User</span><div class=\"modify\"></div></a></td>";
	echo "<td><a id=\"userid_" . $userId . "\" href=\"#\"  onClick=\"javascript:deleteDialog('Delete User', '" . $handlerPage . "','" . $handlerPage . "'," . $userId . ",'updateDetails()')\"><span>Remove User</span><div class=\"delete\"></div></a></td>";
	echo ("</tr>");
	$i ++;
}
?>
	</tbody>
		</table>
	</div>
</div>
<?php
	include("footer.php");
?>